<?php


namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\DurationRecord;
use app\admin\model\App;
use app\admin\model\Game;
use think\Db;

class Duration extends Api
{

    protected $noNeedLogin = ['sst','et'];
    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /*
     * 保存开始时间
     * */
   public function sst()
   {
       $game_id = $this->request->param('game_id');
       $uid = $this->auth->id;

       $data =  $this->auth->getUserinfo();
       $app_id = $data['app_id'];

       $game = Game::where('id', $game_id)->find();
       if (!$game) {
           $this->error('Invalid game');
       }
       $data = array(
           'app_id' => $app_id,
           'game_id' => $game_id,
           'uid' => $uid,
           'sst' => time(),
           'addtime' => date('Y-m-d H:i:s',time())
       );

       $id =  DurationRecord::insertGetId($data);
       $this->success('Saved successfully', ['id' => $id]);
   }

    /*
     * 保存结束时间
     * */
    public function et()
    {
        $id = $this->request->param('id');
        $record = DurationRecord::find($id);
        if (!$record) {
            $this->error('Invalid Id');
        }
        $record->et = time();
        $record->duration = $record->et - $record->sst;
        $record->save();
        $this->success('Saved successfully');
    }
}